$(function () {
    var queryUrl = '../devretreadrecord/list?type=2';
    var Id = getQueryString('Id');
    if(Id){
        queryUrl += '&Id=' + Id;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '工厂核算价值', name: 'accCost', index: 'ACC_COST', width: 80},
            {label: '申报节约价值', name: 'lessCost', index: 'LESS_COST', width: 80},
            {label: '维修成本', name: 'primeCost', index: 'PRIME_COST', width: 80},
            {label: '零件原值', name: 'origVal', index: 'ORIG_VAL', width: 80},
            {label: '物料编码', name: 'matterCode', index: 'MATTER_CODE', width: 80},
            /*{
                label: '类型', name: 'type', index: 'TYPE', width: 80,
                formatter: function (value) {
                    return sysEnums.RECORD_TYPE[value];
                }
            },*/
            {label: '数量', name: 'retNum', index: 'RET_NUM', width: 80},
            {label: '单位', name: 'unit', index: 'UNIT', width: 80},
            {label: '项目名称', name: 'proName', index: 'PRO_NAME', width: 80},
            {label: '项目ID', name: 'proId', index: 'PRO_ID', width: 80,hidden: true},
            {label: '实施单位', name: 'expUnit', index: 'EXP_UNIT', width: 80},
            {label: '维修人员名称', name: 'repairUserName', index: 'REPAIR_USER_NAME', width: 80},
            {label: '备注信息', name: 'remark', index: 'REMARK', width: 80},
            {label: '审核人',hidden:true, name: 'approvalName', index: 'APPROVAL_USER', width: 80},
            {label: '审核状态',hidden:true, name: 'approvalStatus', index: 'APPROVAL_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {label: '审核时间',hidden:true, name: 'approvalTime', index: 'APPROVAL_TIME', width: 80,
                formatter: function (value) {
                    return transDate(value,"yyyy-MM-dd");
                }
            },
            {label: '创建时间',hidden:true, name: 'createDate', index: 'CREATE_DATE', width: 80,
                formatter:function(value){
                    return transDate(value,"yyyy-MM-dd");
                }
            },
            {
                label: '审核详情', name: 'devCode', index: 'DEV_CODE', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true}]
    });
    vm.getSparePartUnit();
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devRetreadRecord: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        partUnitList: [] // 备件单位列表，用于新增或修改备件时单位的下拉选择
    },
    methods: {
        getSparePartUnit: function(){
            // 获取备件单位
            Ajax.request({
                url: '../sparepart/listUnit',
                async: false,
                successCallback: function (r) {
                    vm.partUnitList = r.sparePartUnits;
                }
            });
        },
        //审核详情
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if(!data.approvalName){data.approvalName="-"}
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>审核人：</td><td>"+data.approvalName+"</td></tr>" +
                "<tr><td>审核状态：</td><td>"+sysEnums.CHECK_FLAG[data.approvalStatus]+"</td></tr>" +
                "<tr><td>审核时间：</td><td>"+transDate(data.approvalTime, "yyyy-MM-dd hh:mm")+"</td></tr>" +
                "<tr><td>创建时间：</td><td>"+transDate(data.createDate, "yyyy-MM-dd hh:mm")+"</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        check:function(){
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            var rows = getSelectedRowDatas("#jqGrid");
            var flag = 0;
            for (var j = 0; j<rows.length; j++){
                if (rows[j].approvalStatus === '已审核') {
                    flag++;
                }
            }

            if (flag !== 0){
                alert('有审核已通过,请勿重复审核!', function (index) {
                    vm.reload();
                });
                return;
            }

            confirm('确定要审核选中的记录？', function () {
                Ajax.request({
                    url: "../devretreadrecord/check" ,
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('审核成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devRetreadRecord = {'type': 2};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.devRetreadRecord.id == null  ? "../devretreadrecord/save" : "../devretreadrecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devRetreadRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null){
                return;
            }
            vm.devRetreadRecord = {'delFlag': 0, 'id': id};
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devretreadrecord/updateDel",
                    params: JSON.stringify(vm.devRetreadRecord ),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function(id){
            Ajax.request({
                url: "../devretreadrecord/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.devRetreadRecord = r.devRetreadRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function() {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});